﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

namespace FOBVT.Web.FOBVT2012.en
{
	public partial class Apply : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void BtnSubmit_Click(object sender, EventArgs e)
		{
			Submit("insert");
		}

		private void Submit(string type)
		{
			System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBVTweb.mdb"));
			FOBVT.Web.MainClass.DBClass DBClassTmp = new FOBVT.Web.MainClass.DBClass();
			ArrayList sql = new ArrayList();
			sql.Clear();

			string organization = txtOrganization.Text.Trim();
			string organizationCategory = rdbCategory.SelectedValue.Trim();
			string otherCategory = txtCategoryOther.Text;
			string category = string.Compare(organizationCategory.Trim(), "OTHER (PLEASE DESCRIBE BELOW)", true) == 0 ? otherCategory : organizationCategory;
			string organizationDescription = txtDescription.Text.Trim();
			string name = txtName.Text.Trim();
			string title = txtTitle.Text.Trim();
			string company = txtCompany.Text.Trim();
			string address = txtAddress.Text.Trim();
			string signature = txtSignature.Text.Trim();
			string signatrueTitle = txtSignatureTitle.Text.Trim();
			string phone = txtPhone.Text.Trim();
			string facsimile = txtFacsimile.Text.Trim();
			string email = txtEmail.Text.Trim();
			string website = txtWebsite.Text.Trim();
            string data = TxtData.Text.Trim();
			string date = System.DateTime.Now.ToString();

			
			int varCustomerID = 0;
			string strCustomerID = "";
			if ((this.hdModifyID.Value.Length > 0) || (type == "Modify"))
			{
				varCustomerID = Convert.ToInt32(this.hdModifyID.Value.ToString());
				sql.Add("UPDATE  ApplyMember SET Organization='" + organization +
					"',OrganizationCategory='" + category +
					"',OrganizationDescription='" + organizationDescription +
					"',Name='" + name +
					"',Title='" + title +
					"',Company='" + company +
					"',Address='" + address +
					"',Signature='" + signature +
					"',SignatureTitle=" + signatrueTitle +
					"',Phone='" + phone +
					"',Facsimile='" + facsimile +
					"',Email='" + email +
					"',Website='" + website +
                    "',data='" + data +
					" WHERE (ApplyMemberID ='" + this.hdModifyID.Value + "')");
			}
			else
			{
				varCustomerID = DBClassTmp.GetIDInfo("SELECT max(CLng(Right(ApplyMemberID,len(ApplyMemberID)-Instr(1,ApplyMemberID,'V',1)))) FROM ApplyMember", Conn) + 1;

                strCustomerID = System.DateTime.Now.Year.ToString()+ "FOBTV" + varCustomerID.ToString();
				if (!CheckUserInfoExists())
				{
					sql.Add("INSERT INTO ApplyMember(ApplyMemberID,Organization,OrganizationCategory,OrganizationDescription,Name,Title,Company,Address,Signature,SignatureTitle,Phone,Facsimile,Email,Website,data,ApplyDate)VALUES ('" +
						strCustomerID + "','" +
						organization + "','" +
						category + "','" +
						organizationDescription + "','" +
						name + "','" +
						title + "','" +
						company + "','" +
						address + "','" +
						signature + "','" +
						signatrueTitle + "','" +
						phone + "','" +
						facsimile + "','" +
						email + "','" +
						website + "','" +
                        data + "','" +
						System.DateTime.Now.ToString() + "')");
				}
				else
				{
					FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "This user exists!");
					return;
				}

			}
			if (DBClassTmp.ExcuteSQL(sql, Conn))
			{
				if ((this.hdModifyID.Value.Length > 0) || (type == "Modify"))
				{
					FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Modify successfully!!");
				}
				else
				{
                    FOBVT.Web.MainClass.WebMessageBox.ShowAndRedirect(this.Page, "Congratulate you! Apply successfully!","participation.aspx");

                }

             
			}
			else
			{
				FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Sorry,Operation Failed!!");
			}
		}

		/// <summary>
		/// 注册时，检查用户的Email是否存在
		/// </summary>
		/// <returns></returns>
		public bool CheckUserInfoExists()
		{
			string strSQL = "SELECT count(*) FROM ApplyMember WHERE (Email='" + txtEmail.Text.Trim() + "')";
			System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBVTweb.mdb"));
			DataSet ds = new DataSet();
			MainClass.DBClass DBClassTmp = new MainClass.DBClass();
			ds = DBClassTmp.GetDataSet(strSQL, Conn);
			if (Convert.ToInt32(ds.Tables[0].Rows[0][0]) == 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}

        //protected void lbtMou_Click(object sender, EventArgs e)
        //{
        //    string path;
        //    try
        //    {
        //        path = Request.PhysicalApplicationPath + "PDF/MOU.pdf";// "/" + Session["pdfpath"].ToString() + "/PDF/" + Session["mlmc"].ToString() + ".pdf";
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //    System.IO.Stream iStream = null;
        //    byte[] buffer = new Byte[10000];
        //    int length;
        //    long dataToRead;
        //    string filename = "MOU..pdf";

        //    try
        //    {
        //        iStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
        //        dataToRead = iStream.Length;
        //        Response.Clear();
        //        Response.ClearHeaders();
        //        Response.ClearContent();
        //        Response.ContentType = "application/pdf"; //文件类型  
        //        Response.AddHeader("Content-Length", dataToRead.ToString());//添加文件长度，进而显示进度  
        //        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
        //        while (dataToRead > 0)
        //        {
        //            if (Response.IsClientConnected)
        //            {
        //                length = iStream.Read(buffer, 0, 10000);
        //                Response.OutputStream.Write(buffer, 0, length);
        //                Response.Flush();

        //                buffer = new Byte[10000];
        //                dataToRead = dataToRead - length;
        //            }
        //            else
        //            {
        //                dataToRead = -1;
        //            }
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("error!");
        //    }
        //    finally
        //    {
        //        if (iStream != null)
        //        {
        //            iStream.Close();
        //        }
        //        //结束响应，否则将导致网页内容被输出到文件，进而文件无法打开  
        //        Response.End();
        //    }
        //}

        //protected void lbtApplication_Click(object sender, EventArgs e)
        //{
        //    string path;
        //    try
        //    {
        //        path = Request.PhysicalApplicationPath + "PDF/ApplicationForm.pdf";// "/" + Session["pdfpath"].ToString() + "/PDF/" + Session["mlmc"].ToString() + ".pdf";
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //    System.IO.Stream iStream = null;
        //    byte[] buffer = new Byte[10000];
        //    int length;
        //    long dataToRead;
        //    string filename = "ApplicationForm..pdf";

        //    try
        //    {
        //        iStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
        //        dataToRead = iStream.Length;
        //        Response.Clear();
        //        Response.ClearHeaders();
        //        Response.ClearContent();
        //        Response.ContentType = "application/pdf"; //文件类型  
        //        Response.AddHeader("Content-Length", dataToRead.ToString());//添加文件长度，进而显示进度  
        //        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
        //        while (dataToRead > 0)
        //        {
        //            if (Response.IsClientConnected)
        //            {
        //                length = iStream.Read(buffer, 0, 10000);
        //                Response.OutputStream.Write(buffer, 0, length);
        //                Response.Flush();

        //                buffer = new Byte[10000];
        //                dataToRead = dataToRead - length;
        //            }
        //            else
        //            {
        //                dataToRead = -1;
        //            }
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("error!");
        //    }
        //    finally
        //    {
        //        if (iStream != null)
        //        {
        //            iStream.Close();
        //        }
        //        //结束响应，否则将导致网页内容被输出到文件，进而文件无法打开  
        //        Response.End();
        //    }
        //}

	}
}